import java.awt.*;
import java.awt.event.*;

import javax.swing.JLabel;

public class DrawingCanvasController implements ModelListener, MouseListener, MouseMotionListener 
{
	protected DrawingCanvasView canvas;
	protected ObjectDrawModel model;
	protected Point startMouse;
	protected Point endMouse;
	protected Point currentMouse;
	protected Color saveColor;
	// Constructor
	public DrawingCanvasController(DrawingCanvasView c, ObjectDrawModel m) 
	{
		canvas=c;
		model = m;
		model.addModelListener(this);
	}
  
	public void ModelUpdated() 
	{
		//null so far
	}

	public void mousePressed(MouseEvent e)  
	{
		// Establish starting point for next drawing
		startMouse = e.getPoint();
		 currentMouse = startMouse;
		    Graphics iBGraphics = canvas.getimageBufferGraphics();
		    saveColor = iBGraphics.getColor( );
		    iBGraphics.setXORMode(Color.lightGray);
		    iBGraphics.setColor(Color.white);
	}

	public void mouseDragged(MouseEvent e)  
	{
		Graphics g = canvas.getimageBufferGraphics();
		
		Point newMouse = e.getPoint(); 
		//canvas.drawLineSegment(startingMousePosition, newMousePosition);
	  
		g.drawLine(startMouse.x, startMouse.y, currentMouse.x, currentMouse.y);
	    

		/* draw new temporary figure */
		g.drawLine(startMouse.x, startMouse.y, newMouse.x, newMouse.y);
		currentMouse = newMouse;
		canvas.repaint();
	}

	// Null listener methods
	public void mouseClicked(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) 
	{
		endMouse = e.getPoint();
	    Graphics2D g = (Graphics2D) canvas.getimageBufferGraphics();

	    /* Erase final temporary figure  */
	    g.drawLine(startMouse.x, startMouse.y, currentMouse.x,  currentMouse.y);
	    
	    /* Return graphics context to normal drawing mode and color */
	    g.setPaintMode();
	    g.setColor(saveColor);

	    /* Draw final"permanent" figure */
	    g.drawLine(startMouse.x, startMouse.y, endMouse.x, endMouse.y);
	    canvas.repaint();  
		
		/*
		canvas.drawLineSegment(startMouse, endMouse);
		EditableShape shape = new LineShape(startMouse.x, startMouse.y, endMouse.x, endMouse.y);
		int width = Math.abs(startMouse.x - endMouse.x);
		int height = Math.abs(startMouse.y - endMouse.y);
		ShapeIcon icon = new ShapeIcon(shape, width, height);
		JLabel label = new JLabel(icon);
		canvas.add(label);
		label.repaint();
		*/
	}
	public void mouseMoved(MouseEvent e) {}
	// END OF CLASS
	
}